package com.naihe.anderson.sms.broadcastReceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;

import com.naihe.anderson.sms.application.NhSmsApp;
import com.naihe.anderson.sms.entity.ContactUser;
import com.naihe.anderson.sms.entity.Nhsms;
import com.naihe.anderson.sms.manager.ContactMgr;
import com.naihe.anderson.sms.manager.NhSmsMgr;
import com.naihe.anderson.sms.util.FileUtil;
import com.naihe.anderson.sms.util.ObjectUtil;

public class ReceiverFilterSmsReceiver extends BroadcastReceiver {

	public static final String ACTION_SMS_RECEIVER = "android.provider.Telephony.SMS_RECEIVED";

	@Override
	public void onReceive(Context context, Intent intent) {
		String actionName = intent.getAction();
		if (actionName.equals(ACTION_SMS_RECEIVER)) {
            Bundle bundle = intent.getExtras();  
            if (bundle != null) {  
                Object[] myOBJpdus = (Object[]) bundle.get("pdus");  
                SmsMessage[] messages = new SmsMessage[myOBJpdus.length];  
                for (int i = 0; i < myOBJpdus.length; i++) {  
                    messages[i] = SmsMessage.createFromPdu((byte[]) myOBJpdus[i]);  
                }  
                
                NhSmsApp nhSmsApp = (NhSmsApp) context.getApplicationContext();
                NhSmsMgr nhSmsMgr = nhSmsApp.getNhSmsMgr();
                ContactMgr contactMgr = nhSmsApp.getContactMgr();
                
                String address = "";
                String allmessage = "";
                
                long nhsmsId = nhSmsMgr.getNhsms_id() + 1;
	        	nhSmsMgr.setNhsms_id(nhsmsId);
	        	
	        	Nhsms nhsms = new Nhsms();
				nhsms.set_id(nhsmsId);
				nhsms.setDate_sent(0);
				nhsms.setError_code(0);
				nhsms.setLocked(0);
				nhsms.setProtocol(0);
				nhsms.setRead(0);
				nhsms.setReply_path_present(0);
				nhsms.setSeen(1);
				nhsms.setStatus(Nhsms.STATUS_NONE);
				nhsms.setSubject("");
				nhsms.setThread_id(-1);
				nhsms.setType(Nhsms.MESSAGE_TYPE_INBOX);
				
                for (SmsMessage currentMessage : messages) {
                	// 获得接收短信的电话号码----对号码进行过滤
					address = ObjectUtil.phoneNumberFormat(currentMessage.getDisplayOriginatingAddress());
					
					nhsms.setDate(currentMessage.getTimestampMillis());
					nhsms.setService_center(currentMessage.getServiceCenterAddress());
					
					allmessage += currentMessage.getDisplayMessageBody();
                }
                
                nhsms.setAddress(address);
                nhsms.setBody(allmessage);
                
                ContactUser contactUser = contactMgr.getContactUserById(contactMgr.getContactIdByPhoneNum(address));
    			if(contactUser != null){
    				nhsms.setPerson(contactUser.getContactId());
    			} else {
    				nhsms.setPerson(0);
    			}
    			
    			FileUtil.addLog("ReceiverFilterSmsReceiver::onReceive=" + nhsms.toString(), ReceiverFilterSmsReceiver.class, "ReceiverFilterSmsReceiver", null);
                nhSmsMgr.insertNewNhsms(nhsms);
			}
		}

	}

}
